<?php
namespace app\admin\controller;

use app\admin\model\ApiWordModel;
use app\admin\model\ApiUserModel;
use app\admin\model\UserModel;
use think\Db;

/**
 * Func 操作类
 *
 * Class ApiWordController
 *
 * @package app\admin\controller
 */
class ApiWordController extends BaseController
{
    /**
     * Func 列表
     */
    public function index()
    {
        // 时间
        if ($this->start_time && $this->end_time)
        {
            $condition['a.create_time'] = ['between', [
                strtotime($this->start_time), strtotime($this->end_time),
            ]];
        }

        // 关键词
       if ($this->keyword)
       {
            if ($this->searchType)
            {
                $condition[$this->searchType] = ['like', '%' . $this->keyword . '%'];
            } else {
                $condition['a.word_no'] = ['like', '%' . $this->keyword . '%'];
            }
        }

        $condition['a.id'] = ['gt', 0];

        // 字段
        $fieldArr = ['a.*'];

        // 连接表
        $joinArr = [];

        // 数据
        $infos['list'] = (new ApiWordModel())->getApiWordListInfo(
            $condition, ['a.id' => 'desc'], ['page' => $this->page, 'limit' => $this->limit],
            $fieldArr, $joinArr
        );

        $infos['idTypeArr'] = self::$wordTypeArr;
        $infos['wordStatusArr'] = self::$wordStatusArr;

        $this->assign('infos', $infos);

        return $this->fetch();
    }

    /**
     * Func 添加
     */
    public function add()
    {
        if ($this->request->isPost())
        {
            $param = $this->request->param();

            $result = $this->validate($param, 'ApiWord.add');
            if (!is_bool($result))
            {
                $this->error($result);
            }

            if (!in_array($param['id_type'], array_keys(self::$wordTypeArr)))
            {
                $this->error('类型值错误');
            }

            // 身份证/护照
            if(in_array($param['id_type'],[1,2]))
            {
                if (!preg_match('/^[^\x{4e00}-\x{9fa5}]+$/u', $param['word_no'])) {
                    $this->error('规则不能包含中文');
                }
            }

            // 添加和更新的字段信息
            $data1 = self::getPostParamInfo(['id_type', 'word_no', 'status', 'word_desc']);

            // 附加参数
            $data2 = [];

            // 添加的数据
            $data = array_merge((array)$data1, (array)$data2);

            $coobj = new ApiWordModel();

            // 查询数据是否存在
            $conditionCC['a.id_type'] = array('eq', intval($param['id_type']));
            $conditionCC['a.word_no'] = array('eq', trim($data['word_no']));
            if ($coobj->getApiWordCount($conditionCC))
            {
                $this->error('数据已存在');
            }

            if ($coobj->addOrUpdateApiWordInfo($data))
            {
                $this->success('添加成功!', url('index', []));
            } else {
                $this->error('添加失败,请稍后重试');
            }
        }

        $detail['idTypeArr'] = self::$wordTypeArr;
        $detail['wordStatusArr'] = self::$wordStatusArr;


        $this->assign('detail', $detail);

        return $this->fetch();
    }

    /**
     * Func 修改
     */
    public function edit()
    {
        $param = $this->request->param();

        $coobj = new ApiWordModel();

        $detail = $coobj->getApiWordOneInfo(['uuid' => $param['uuid']], ['id' => 'desc']);
        if (!$detail)
        {
            $this->error('信息不存在');
        }

        if ($this->request->isPost())
        {
            $result = $this->validate($param, 'ApiWord.edit');
            if (!is_bool($result))
            {
                $this->error($result);
            }

            if (!in_array($param['id_type'], array_keys(self::$wordTypeArr)))
            {
                $this->error('类型值错误');
            }

            // 身份证/护照
            if(in_array($param['id_type'],[1,2]))
            {
                if (!preg_match('/^[^\x{4e00}-\x{9fa5}]+$/u', $param['word_no'])) {
                    $this->error('规则不能包含中文');
                }
            }


            // 添加和更新的字段信息
            $data1 = self::getPostParamInfo(['id_type', 'word_no', 'status', 'word_desc']);

            $data2 = [];

            // 添加的数据
            $data = array_merge((array)$data1, (array)$data2);

            if ($coobj->addOrUpdateApiWordInfo($data, $detail['id']))
            {
                $this->success('修改成功!', url('index', []));
            } else {
                $this->error('修改失败,请稍后重试');
            }
        }

        $detail['idTypeArr'] = self::$wordTypeArr;
        $detail['wordStatusArr'] = self::$wordStatusArr;

        $this->assign('detail', $detail);

        return $this->fetch();
    }


    /**
     * Func 删除
     */
    public function delete()
    {
        $param = $this->request->param();
        if (isset($param['ids']))
        {
            $condition['id'] = ['in', $param['ids']];
            (new ApiWordModel())->deleteApiWordInfo($condition);
            $this->success("删除成功！", '');
        }
    }

}